
import optparse
import os
import sys
import boto

def main():
    p = optparse.OptionParser(usage="%prog [options]")
    p.add_option('--target-bucket', action='store', 
        help="S3 bucket (must exist) you want logs for.  Default: %default")
    p.add_option('--log-bucket', action='store',
        help="S3 bucket (must exist) to store logs in.  Default: %default")
    (options, args) = p.parse_args()
        
    if 'AWS_ACCESS_KEY_ID' not in os.environ:
        raise EnvironmentError("Missing environment variable AWS_ACCESS_KEY_ID")
    if 'AWS_SECRET_ACCESS_KEY' not in os.environ:
        raise EnvironmentError("Missing environment variable AWS_SECRET_ACCESS_KEY")
    conn = boto.connect_s3()
    log_bucket = conn.get_bucket(options.log_bucket)
    log_bucket.set_as_logging_target()
    target_bucket = conn.get_bucket(options.target_bucket)
    target_bucket.enable_logging(log_bucket)
    print "Enabled logging for %s, logs will be in %s" % (options.target_bucket, options.log_bucket)

if __name__ == '__main__':
    main()
